﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using Ecreate.Utility;

namespace Ecreate.Web.utility
{
    public enum LoginUserType
    {
        admin,customer
    }

    public class SessionObject
    {
        private LoginUserType _usertype;
        public LoginUserType Usertype
        {
            get { return _usertype; }
            set { _usertype = value; }
        }


        private Ecreate.Popedom.tSysuser _Admin = null;

        public Ecreate.Popedom.tSysuser Admin
        {
            get { return _Admin; }
            set { _Admin = value; }
        }
        private Ecreate.Model.t_customer _Customer = null;

        public Ecreate.Model.t_customer Customer
        {
            get { return _Customer; }
            set { _Customer = value; }
        }
    }


    public class SessionManage
    {
        public const string SessionObjectName = "_SessionObjectName";
        public static void CheckSessionObject(Page page)
        {
            object adminsession = page.Session[SessionObjectName];
            if(adminsession==null)
            {
                page.Response.Redirect("/popedom/Login/login.aspx");
            }
        }


        public static void ClearSessionObject(System.Web.UI.Page page)
        {
            page.Session.Remove(SessionObjectName);
        }
        public static void ClearSessionObject(System.Web.SessionState.HttpSessionState session)
        {
            session.Remove(SessionObjectName);
        }

        public static void AddSessionObject(System.Web.UI.Page page, Ecreate.Popedom.tSysuser systemUser)
        {
            SessionObject s = new SessionObject();
            s.Admin = systemUser;
            s.Usertype = LoginUserType.admin;
            page.Session.Add(SessionObjectName, s);
        }
        public static void AddSessionObject(System.Web.SessionState.HttpSessionState session, Ecreate.Popedom.tSysuser systemUser)
        {
            SessionObject s = new SessionObject();
            s.Admin = systemUser;
            s.Usertype = LoginUserType.admin;
            session.Add(SessionObjectName, s);
        }


        public static void AddSessionObject(System.Web.UI.Page page, Ecreate.Model.t_customer customer)
        {
            SessionObject s = new SessionObject();
            s.Customer = customer;
            s.Usertype = LoginUserType.customer;
            page.Session.Add(SessionObjectName, s);
        }



        public static void AddSessionObject(System.Web.SessionState.HttpSessionState session , Ecreate.Model.t_customer customer)
        {
            SessionObject s = new SessionObject();
            s.Customer = customer;
            s.Usertype = LoginUserType.customer;
            session.Add(SessionObjectName, s);
        }
        public static SessionObject getSession(System.Web.UI.Page page)
        {
            object result = page.Session[SessionObjectName];
            return (SessionObject)result;
        }

        public static Object getSessionObject(System.Web.UI.Page page)
        {
            object result = page.Session[SessionObjectName];
            return result;
        }

        public static Object getSessionObject(System.Web.SessionState.HttpSessionState session)
        {
            object result = session[SessionObjectName];
            return result;
        }


        public static bool checkSessoin(System.Web.UI.Page page)
        {
            object o = getSessionObject(page);
            if (o == null)
                return false;
            else
                return true;
        }

        public static bool checkSessoin(System.Web.SessionState.HttpSessionState session)
        {
            object o = getSessionObject(session);
            if (o == null)
                return false;
            else
                return true;
        }
    }
}
